
/**
 * @file superTool.cpp
 * @author outbreak_sen (1023786231@qq.com)
 * @brief 找到最合适的HSV,并且输出某个点的HSV;
 * 		输出鼠标单击的点坐标和相应HSV值;
 * 		测试腐蚀膨胀效果;
 * 		超级无敌，赛场必备
 * @version 0.1
 * @date 2021-11-26
 *
 * @copyright Copyright (c) 2021
 *
 */
#include <iostream>
#include "opencv2/opencv.hpp"
#include <opencv2/core.hpp>
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/ml/ml.hpp"
#include "fstream"
#include "../libHardware/RealSense/include/RealSense.hpp"
using namespace cv;
using namespace std;
using namespace wmj;
// #define USEREALSENSE
// #define USEVIDEO
#define USEPICTRUE
//#define GETHSV
class FrameFinder
{
public:
	FrameFinder(){};
	void saveBinaryImg(Mat &, Mat &);

private:
	int H_MIN = 0;
	int H_MAX = 100;
	int S_MIN = 0;
	int S_MAX = 100;
	int V_MIN = 0;
	int V_MAX = 100;
	int erodeSquree = 3;
	int dilateSquree = 3;
	Mat element_1;
	Mat element_2;
	void createBars();
	// void on_trackbar(int, void*);
};
void FrameFinder::saveBinaryImg(Mat &imgOriginal, Mat &imgDst)
{
	// namedWindow("HSVcontrol");
	createBars();
	if (erodeSquree < 3)
		erodeSquree = 3;
	if (dilateSquree < 3)
		dilateSquree = 3;
	imgOriginal.copyTo(imgDst);
	// while (true) {
	cvtColor(imgOriginal, imgDst, CV_BGR2HSV);
	// cvtColor(imgOriginal, imgDst, CV_BGR2HLS);
	element_1 = getStructuringElement(MORPH_RECT, Size(erodeSquree, erodeSquree));
	element_2 = getStructuringElement(MORPH_RECT, Size(dilateSquree, dilateSquree));
	inRange(imgDst, Scalar(H_MIN, S_MIN, V_MIN), Scalar(H_MAX, S_MAX, V_MAX), imgDst);

	cv::erode(imgDst, imgDst, element_1);
	cv::dilate(imgDst, imgDst, element_2);
	std::cout << " Scalar(H_MIN, S_MIN, V_MIN)" << Scalar(H_MIN, S_MIN, V_MIN) << std::endl;
	std::cout << "Scalar(H_MAX, S_MAX, V_MAX)" << Scalar(H_MAX, S_MAX, V_MAX) << std::endl;
	std::cout << " erodeSquree" << erodeSquree << std::endl;
	std::cout << "SdilateSquree" << dilateSquree << std::endl;
	// }
	// imshow("imshow",imgDst);
};
void FrameFinder::createBars()
{
	namedWindow("trackbars", 0);
	createTrackbar("H_MIN", "trackbars", &H_MIN, 255, NULL);
	createTrackbar("H_MAX", "trackbars", &H_MAX, 255, NULL);
	createTrackbar("s_MIN", "trackbars", &S_MIN, 255, NULL);
	createTrackbar("s_MAX", "trackbars", &S_MAX, 255, NULL);
	createTrackbar("v_MIN", "trackbars", &V_MIN, 255, NULL);
	createTrackbar("v_MAX", "trackbars", &V_MAX, 255, NULL);
	createTrackbar("erodeSquree", "trackbars", &erodeSquree, 40, NULL);
	createTrackbar("dilateSquree", "trackbars", &dilateSquree, 40, NULL);
};
void outPutPoint(int event, int x, int y, int flags, void *ustc)
{
	if (event == EVENT_LBUTTONDOWN)
	{
		cout << "the point is :" << x << " " << y << endl;
	}
}
int main(int argc, char **argv)
{
	Mat origin, imgDst;
	FrameFinder FrameFinder;
#ifdef USEPICTRUE
	origin = imread(argv[1]);
	cv::Mat result;
	//1170 2530
	cv::resize(origin,origin,cv::Size(1170/2,2530/2));
	while (true)
	{
		FrameFinder.saveBinaryImg(origin, imgDst);
		imshow("origin", origin);
		imshow("HSVcontrol", imgDst);
		
        bitwise_not(imgDst,result);//颜色反转
	
        imshow("result",result);
		if (waitKey(2) == 'q')
		{
			break;
		}
	}
#endif
#ifdef USEVIDEO
	VideoCapture cap = VideoCapture(argv[1]);
	cap.open(argv[1]);
	// VideoCapture cap;
	// cap.open(1);
	// int camera_id = 1;
	// std::cout << "Enter the camera id :" << std::endl;
	// std::cin >> camera_id;
	// cv::VideoCapture cap;
	// for (int i = camera_id; i < 10; i++)
	// // for (int i = 1; i < 10; i++)
	// {
	// 	std::string camera_name = "/dev/video" + std::to_string(i);
	// 	cap.open(camera_name);
	// 	if (cap.isOpened())
	// 		break;
	// 	if (i == 9)
	// 	{
	// 		std::cout << "failed to open camera" << std::endl;
	// 		return -1;
	// 	}
	// }
	// cap.set(4, 640);
	// cap.set(5, 480);
	// cap.set(7, cv::VideoWriter::fourcc('M', 'P', 'E', 'G'));
	// cap.set(cv::CAP_PROP_AUTO_EXPOSURE, 0.25);
	// cap.set(cv::CAP_PROP_EXPOSURE, 2);
	while (cap.read(origin))
	{
		// cap>>origin;
		imshow("origin", origin);
		setMouseCallback("origin", outPutPoint);

		FrameFinder.saveBinaryImg(origin, imgDst);
		imshow("HSVcontrol", imgDst);
		if (waitKey(0) == 'q')
		{
			break;
		}
	}
	return 0;
#endif
#ifdef USEREALSENSE
	std::shared_ptr<wmj::RealSense> rs = std::make_shared<wmj::RealSense>();
	while (rs->getColorImage(origin))
	{
		imshow("origin", origin);
		setMouseCallback("origin", outPutPoint);
		FrameFinder.saveBinaryImg(origin, imgDst);
		imshow("HSVcontrol", imgDst);
		if (waitKey(20) == 'q')
		{
			break;
		}
	}
	return 0;
#endif
#ifdef GETHSV
	double m_H = origin.at<Vec3b>(320, 240)[0];
	double m_S = origin.at<Vec3b>(320, 240)[1];
	double m_V = origin.at<Vec3b>(320, 240)[2];
	cout << m_H << ' ' << m_S << ' ' << m_V << ' ' << endl;
	return 0;
#endif
}
